// pages/login-kjdl/login-kjdl.js
var util = require('../../utils/network.js')
var uuidv1 = require('../../utils/node-uuid/uuidv1');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        getcode: '获取验证码',
        imgurl: '',//图片验证码
        tel: '',//输入手机号
        imgcode: '',//输入图片验证码
        numcode: '',//输入数字验证码
        message: '',//错误信息
        bShow: false,//错误信息显示隐藏
        uid: ''
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        var uid = uuidv1()
        that.setData({
            uid: uid
        })
        //函数调用  
        wx.request({
            url: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS',
            data: {
                "uuid": uid,
                "devicetoken": "",
                "clienttoken": "",
                "clienttype": "weix",
                "version": "1.0"
            },
            header: {
                'content-type': ' text/html'
            },
            method: "GET",
            dataType: "json",
            success: function (res) {
                that.setData({
                    imgurl: util.main +'/imagevalidate/createValidateImageToAndroidOrIOS?uuid=' + uid
                })
            }
        })
    },
    //   输入手机号
    tel: function (e) {
        this.setData({
            tel: e.detail.value
        })
    },
    //   输入图片验证码
    imgCode: function (e) {
        this.setData({
            imgcode: e.detail.value
        })
    },
    //   输入6位数字
    numCode: function (e) {
        this.setData({
            numcode: e.detail.value
        })
    },
    //   改变验证码图片
    changeImg: function () {
        var that = this
        var uid = uuidv1()
        that.setData({
            uid: uid
        })
        //函数调用  
        wx.request({
            url: util.main + '/imagevalidate/createValidateImageToAndroidOrIOS',
            data: {
                "uuid": uid,
                "token": {
                    "devicetoken": "",
                    "clienttoken": "",
                    "clienttype": "weix",
                    "version": "1.0"
                }
            },
            method: "GET",
            dataType: "json",
            success: function (res) {
                that.setData({
                    imgurl: util.main +'/imagevalidate/createValidateImageToAndroidOrIOS?uuid=' + uid
                })
            }
        })
    },
    //   获取验证码
    getcode: function () {
        var that = this
        var uid = that.data.uid
        var tel = that.data.tel
        var imgcode = that.data.imgcode
        var openId = wx.getStorageSync("openId")
        var token = '{"devicetoken": "","clienttoken": "","clienttype": "android","version": "1.0"}'
        if (tel == '') {
            that.setData({
                message: '请输入正确的手机号',
                bShow: true
            })
        } else if (imgcode == '') {
            that.setData({
                message: '请输入正确的图片验证码',
                bShow: true
            })
        } else if (tel != '' && imgcode != '') {
            wx.request({
                url: util.main + '/sms/association',
                data: "phone=" + that.data.tel + "&type=QQ&openid=" + openId + "&deviceid=" + openId + "&randomverifyCode=" + that.data.imgcode + "&token=" + token + "&uuid=" + uid,
                header: {
                    "Content-Type": "application/x-www-form-urlencoded"
                },
                method: "POST",
                success: function (res) {
                    console.log(res)
                    if (res.statusCode == 200) {
                        var wait = 181
                        var time = setInterval(function () {
                            wait -= 1
                            that.setData({
                                getcode: wait + 's',
                                disabled: true
                            })
                            if (wait < 1) {
                                wait == 10
                                clearInterval(time)
                                that.setData({
                                    getcode: '获取验证码',
                                    disabled: false
                                })
                            }
                        }, 1000)
                        that.setData({
                            message: res.data.message,
                            bShow: true
                        })
                    } else {
                        that.setData({
                            message: res.data[0].message,
                            bShow: true
                        })
                    }
                    if (that.data.bShow) {
                        setTimeout(function () {
                            that.setData({
                                bShow: false
                            })
                        }, 2000)
                    }
                }
            })
        }
        if (that.data.bShow) {
            setTimeout(function () {
                that.setData({
                    bShow: false
                })
            }, 2000)
        }
    },
    // 登录
    formSubmit: function () {
        var that = this
        var uid = that.data.uid
        var token = '{"devicetoken": "","clienttoken": "","clienttype": "android","version": "1.0"}'
        wx.request({
            url: util.main + '/user/association',
            data: {
                "phone": that.data.tel,
                "checkcode": that.data.numcode,
                "deviceid": wx.getStorageSync("openId"),
                "token": token,
                "uuid": uid

            },
            header: {
                "Content-Type": "application/x-www-form-urlencoded"
            },
            method: "POST",
            success: function (res) {
                console.log(res)
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})